package com.test.config;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDecisionVoter;
import org.springframework.security.access.vote.UnanimousBased;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.web.access.expression.WebExpressionVoter;

import java.util.ArrayList;
import java.util.List;
import java.util.UnknownFormatConversionException;

/**
 * @Author lhc
 * @Date 2021/8/12 10:25
 */
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    @Autowired
    private TokenStore redisTokenStore;

    @Value("${lhc.oauth.resourceId}")
    private String resourceId;

    @Value("${lhc.oauth.noMatcher}")
    private String noMatcher;


    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources
                .resourceId(resourceId) //和授权配置保持一致
                .tokenStore(redisTokenStore)
        ;
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        String[] urls = null;
        if (StringUtils.isNotBlank(this.noMatcher)) {
            urls = this.noMatcher.split(",");
        }
//
//        if (urls != null) {
//            ((AuthorizedUrl)((AuthorizedUrl)http.authorizeRequests()
//                    .antMatchers(urls)).permitAll()                               //usrls是nomathers里面的内容，都放行
//                    .antMatchers(new String[]{this.antMather})).authenticated()  //antMather里面的要认证
//                    .accessDecisionManager(this.accessDecisionManager());
//        } else {
//            ((AuthorizedUrl)http.authorizeRequests()
//                    .antMatchers(new String[]{this.antMather})).authenticated()  //拦截antMatcher请求
//                    .accessDecisionManager(this.accessDecisionManager());
//        }
        assert urls != null;
        http.authorizeRequests()
                .antMatchers(urls).permitAll()
                .anyRequest().authenticated()
        ;
    }

    @Bean
    public AccessDecisionManager accessDecisionManager() {
        List<AccessDecisionVoter<?>> decisionVoters = new ArrayList<>();
        decisionVoters.add(new WebExpressionVoter());
        return new UnanimousBased(decisionVoters);
    }
}